package com.vitabuybuy.tv.ui.home.bidd

import android.os.Bundle
import androidx.core.os.bundleOf
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityBiddingRecordBinding
import com.vitabuybuy.tv.ui.home.bidd.viewmodel.BiddingZoneViewModel
import kotlinx.android.synthetic.main.activity_bidding_record.*

class BiddingRecordActivity : BaseActivity<BiddingZoneViewModel, ActivityBiddingRecordBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_bidding_record

    private var pageNumber = 1

    private var biddingId: Int? = -1

    override fun onGetBundle(bundle: Bundle?) {
        biddingId = bundle?.getInt("biddingId")
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.bid_record_2))
        mBinding.viewModel = viewModel
    }

    override fun observeData() {
        viewModel.getBidderList(biddingId, pageNumber, 10)
    }
    override fun eventHandling() {
        refresh.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber++
                viewModel.getBidderList(biddingId, pageNumber, 10)
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber = 1
                viewModel.getBidderList(biddingId, pageNumber, 10)
            }
        })
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            0 -> {
                pageNumber = 1
                viewModel.getBidderList(biddingId, pageNumber, 10)
            }
            1 -> {
                refresh.onLoad(msg.arg1)
            }
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>, biddingId: Int?) {
            mContext.startActivity(
                bundleOf(Pair("biddingId", biddingId)),
                BiddingRecordActivity::class.java
            )
        }
    }
}
